package com.ruoyi.project.system.controller;

import java.net.URLDecoder;
import java.util.Date;
import java.util.List;

import com.ruoyi.framework.aspectj.lang.annotation.Log;
import com.ruoyi.framework.aspectj.lang.enums.BusinessType;
import com.ruoyi.framework.web.controller.BaseController;
import com.ruoyi.framework.web.domain.AjaxResult;
import com.ruoyi.framework.web.page.TableDataInfo;
import com.ruoyi.project.system.domain.QuestionStore;
import com.ruoyi.project.system.service.IQuestionStoreService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.utils.poi.ExcelUtil;

/**
 * [题库]Controller
 * 
 * @author ruoyi
 * @date 2020-10-07
 */
@RestController
@RequestMapping("/system/questionStore")
public class QuestionStoreController extends BaseController
{
    @Autowired
    private IQuestionStoreService questionStoreService;

    /**
     * 查询[题库]列表
     */
    @PreAuthorize("@ss.hasPermi('system:store:list')")
    @GetMapping("/list")
    public TableDataInfo list(QuestionStore questionStore)
    {
        startPage();
        List<QuestionStore> list = questionStoreService.selectQuestionStoreList(questionStore);
        return getDataTable(list);
    }

    /**
     * 导出[题库]列表
     */
    @PreAuthorize("@ss.hasPermi('system:store:export')")
    @Log(title = "[题库]", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(QuestionStore questionStore)
    {
        List<QuestionStore> list = questionStoreService.selectQuestionStoreList(questionStore);
        ExcelUtil<QuestionStore> util = new ExcelUtil<QuestionStore>(QuestionStore.class);
        return util.exportExcel(list, "store");
    }

    /**
     * 获取[题库]详细信息
     */
    @PreAuthorize("@ss.hasPermi('system:store:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Integer id)
    {
        return AjaxResult.success(questionStoreService.selectQuestionStoreById(id));
    }

    /**
     * 新增[题库]
     */
    @PreAuthorize("@ss.hasPermi('system:store:add')")
    @Log(title = "[题库]", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody QuestionStore questionStore)
    {
        questionStore.setContent(URLDecoder.decode(questionStore.getContent()));
        if(questionStore.getTypeCode()==1){
            questionStore.setTypeName("判断题");
        }else if(questionStore.getTypeCode()==2){
            questionStore.setTypeName("单选题");
        }else if(questionStore.getTypeCode()==3){
            questionStore.setTypeName("多选题");
        }else if(questionStore.getTypeCode()==4){
            questionStore.setTypeName("填空题");
        }else if(questionStore.getTypeCode()==5){
            questionStore.setTypeName("问答题");
        }
        questionStore.setSuggestAnswer(questionStore.getStandartAnswer());
        if(questionStore.getTypeCode()==1||questionStore.getTypeCode()==2||questionStore.getTypeCode()==3){

            String options = URLDecoder.decode(questionStore.getOptions());
            questionStore.setOptions(options.substring(0,options.length()-3));
        }

        questionStore.setCreateDt(new Date());
        return toAjax(questionStoreService.insertQuestionStore(questionStore));
    }

    /**
     * 修改[题库]
     */
    @PreAuthorize("@ss.hasPermi('system:store:edit')")
    @Log(title = "[题库]", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody QuestionStore questionStore)
    {
        questionStore.setContent(URLDecoder.decode(questionStore.getContent()));
        questionStore.setSuggestAnswer(questionStore.getStandartAnswer());
        if(questionStore.getTypeCode()==1){
            questionStore.setTypeName("判断题");
        }else if(questionStore.getTypeCode()==2){
            questionStore.setTypeName("单选题");
        }else if(questionStore.getTypeCode()==3){
            questionStore.setTypeName("多选题");
        }else if(questionStore.getTypeCode()==4){
            questionStore.setTypeName("填空题");
        }else if(questionStore.getTypeCode()==5){
            questionStore.setTypeName("问答题");
        }
        if(questionStore.getTypeCode()==1||questionStore.getTypeCode()==2||questionStore.getTypeCode()==3){
            String options = URLDecoder.decode(questionStore.getOptions());
            questionStore.setOptions(options.substring(0,options.length()-3));
        }

        questionStore.setUpdateTime(new Date());
        return toAjax(questionStoreService.updateQuestionStore(questionStore));
    }

    /**
     * 删除[题库]
     */
    @PreAuthorize("@ss.hasPermi('system:store:remove')")
    @Log(title = "[题库]", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Integer[] ids)
    {
        return toAjax(questionStoreService.deleteQuestionStoreByIds(ids));
    }
}
